<?php

namespace App\Services\Plaza;

use App\Models\AreaJobPositionModel;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;

class PlazaService
{
    public function list(Authenticatable|User|null $user)
    {
        return PlazaModel::query()
            ->with(['jobPositionInfo'])
            ->where('cur_status',1)
            ->whereHas('jobPositionInfo', function (Builder $query) use ($user) {
                $query->validPositions();
//                if ($user) {
//                    //登录用户做年龄性别限制
//                    $age = $user->age();
//                    if ($age != -1) {
//                        $query->whereRaw("((min_age = 0) or (min_age <= $age))")->whereRaw("((max_age =0) or (max_age >= $age))");
//                    }
//                    //登录用户做性别限制
//                    if ($user->gender > 0) {
//                        $query->whereRaw("((gender_requirement = 3) or (gender_requirement = $user->gender))");
//                    }
//                }
            })->orWhere('cur_type', PlazaModel::TYPE_AREA)
            ->orderBy('sort')
            ->paginate();
    }

    /**
     * @param Authenticatable|User|null $user
     * @param int $plaza_id
     * @return LengthAwarePaginator
     */
    public function areaList(Authenticatable|User|null $user,int $plaza_id)
    {
        $idArray = AreaJobPositionModel::query()->orderBy('sort')->where('plaza_id',$plaza_id)->pluck('job_position_id')->toArray();
        $now = now();
        $sortString =  implode(',',$idArray);
        $query = JobpositionModel::query()
            ->with([
                'categories',
                'company',
            ])
            ->whereIn('id',$idArray)->validPositions()
            ->orderByRaw("field(`id`,$sortString)");
//        if ($user) {
//            //登录用户做年龄性别限制
//            $age = $user->age();
//            if ($age != -1) {
//                $query->whereRaw("((min_age = 0) or (min_age <= $age))")->whereRaw("((max_age =0) or (max_age >= $age))");
//            }
//            //登录用户做性别限制
//            if ($user->gender > 0) {
//                $query->whereRaw("((gender_requirement = 3) or (gender_requirement = $user->gender))");
//            }
//        }
        return $query->paginate();
    }
}